﻿

#region USING
//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
//
#endregion USING


namespace Gsp315_AIBehaviors
{
    public class Controller
    {
        #region DATA_MEMBERS
        //
        private int maxRock, maxPaper, maxScissor;
        private static List<Agent> aliveAgents;
        //
        #endregion DATA_MEMBERS



        #region OBJECT_CONSTRUCTION
        //
        //-------------------------------------------------------------------------
        //Default Contructor
        public Controller(Agent rock, Agent paper, Agent scissor)
        {
            //Max # of agents...
            maxRock = maxPaper = maxScissor = 3;

            aliveAgents = new List<Agent>(); //instantiate Alive Agent List
            init(rock, paper, scissor);      //Init Rock, Paper, Scissor Agents
        }

        //-------------------------------------------------------------------------
        //Initial agents to load on screen by default, when program launches
        private void init(Agent r, Agent p, Agent s)
        {
            Agent rock, paper, scissor;

            for (int i = 0; i < maxRock; ++i)
            {
                rock = new Agent(r.Texture); //, r.Position, r.Size);
                paper = new Agent(p.Texture); //, p.Position, p.Size);
                scissor = new Agent(s.Texture); //, s.Position, s.Size);

                //Set Agent Index...
                rock.Index = paper.Index = i;//scissor.Index = i;

                //Update Agent Type...
                rock.AgentType = agent_Type.ROCK;
                paper.AgentType = agent_Type.PAPER;
                scissor.AgentType = agent_Type.SCISSOR;

                //Random Position...
                rock.Position = Helper.Position_Random(10, 760, 10, 580);  //(xPosSeed, yPosSeed, 40, 760, 40, 560);
                paper.Position = Helper.Position_Random(10, 760, 10, 580);  //(xPosSeed, yPosSeed, 40, 760, 40, 560); 
                scissor.Position = Helper.Position_Random(10, 760, 10, 580);  //(xPosSeed, yPosSeed, 40, 760, 40, 560);

                //Random Angle of Movement...
                //obj direction in vector is internally updated, when angleInRadion is updated
                rock.AngleInRadians = Helper.AngleInRadians_Random();
                paper.AngleInRadians = Helper.AngleInRadians_Random();
                scissor.AngleInRadians = Helper.AngleInRadians_Random();

                aliveAgents.Add(rock);
                aliveAgents.Add(paper);
                aliveAgents.Add(scissor);
            }
        }
        //
        #endregion OBJECT_CONSTRUCTION



        #region PROPERTIES
        //
        ////public int RockScore { get { return maxRock; } }
        //
        ////public int PaperScore { get { return maxPaper; } }
        //
        ////public int ScissorScore { get { return maxScissor; } }
        //
        public List<Agent> AliveAgents { get { return aliveAgents; } }
        
        //Read-Only access 'AliveAgents' List
        public static List<Agent> EntityList_Alive { get { return aliveAgents; } }
        //
        #endregion PROPERTIES



        //-------------------------------------------------------------------------
        //Spawn an agent -> Called in Update Method
        private Agent SpawnAgent(Agent agent)
        {
            agent.IsAlive = true;
            agent.Position = Helper.Position_Random(50, 750, 50, 550);
            agent.AngleInRadians = Helper.AngleInRadians_Random();
            
            return agent;
        }


        //-------------------------------------------------------------------------
        //Update DeadAgents, AliveAgents List, Program States, and Agent(s) Stats
        public void Update(GameTime gameTime)
        {
            //foreach (Player agent in aliveAgents)
            for (int i = 0; i < aliveAgents.Count; ++i)
            {
                aliveAgents[i].Update_Behaviour(ref aliveAgents, gameTime);

                if (!aliveAgents[i].IsAlive)//IF agent=>DEAD
                {
                    aliveAgents[i] = SpawnAgent(aliveAgents[i]);
                }
            }
        }

        //-------------------------------------------------------------------------
        //Render AliveAgents, Agent(s) Stats to screen
        public void Draw(SpriteBatch spriteBatch)
        {
            foreach (Agent agent in aliveAgents)
            {
                if (agent.IsAlive) { agent.Draw(spriteBatch); }
            }
        }
    }//class
}//namespace
